package com.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.health.constant.MessageConstant;
import com.health.dao.MemberDao;
import com.health.dao.SetmealDao;
import com.health.entity.PageResult;
import com.health.entity.QueryPageBean;
import com.health.entity.Result;
import com.health.pojo.Member;
import com.health.pojo.Order;
import com.health.pojo.OrderList;
import com.health.pojo.Setmeal;
import com.health.service.OrderService;
import com.health.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @Classname SetmealServiceImpl
 * @Description TODO
 * @Version 1.0.0
 * @Date 2023/3/16 14:47
 * @Author RainGrd
 */
@Service(interfaceClass = SetmealService.class)
@Transactional
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;

    @Autowired
    private OrderService orderService;

    @Autowired
    private MemberDao memberDao;

   /* @Autowired
    private JedisPool jedisPool;*/

    @Override
    public Result saveSetmeal(Setmeal setmeal, Integer[] checkGroupIds) {
        try {
            setmeal.setImg(setmeal.getImg());
            int i = setmealDao.insertSetmeal(setmeal);
            setSetmealAndCheckGroup(setmeal.getId(), checkGroupIds);
            /*//将图片名称保存到Redis
            savePic2Redis(setmeal.getImg());*/
            return i > 0 ? Result.success(MessageConstant.ADD_SETMEAL_SUCCESS) : Result.error(MessageConstant.ADD_SETMEAL_FAIL);
        } catch (Exception e) {
            return Result.error();
        }
    }

    @Override
    public PageResult querySetmealPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<Setmeal> setmealList = setmealDao.selectByCondition(queryPageBean.getQueryString());
        log.info("查询出来的数据列表:{}", setmealList);
        long count = setmealDao.selectTotalByCondition(queryPageBean.getQueryString());
        log.info("查询出来的总数:{}", count);
        return new PageResult(count, setmealList);
    }

    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return setmealDao.querySetmealCount();
    }

    @Override
    public Result queryAll() {
        return Result.success(setmealDao.selectAll());
    }

    @Override
    public Result editSetmeal(Setmeal setmeal, Integer[] checkGroupIds) {
        log.info("setmeal:{}", setmeal);
        try {
            //根据检查组id删除中间表数据（清理原有关联关系）
            setmealDao.deleteAssociation(setmeal.getId());
            setSetmealAndCheckGroup(setmeal.getId(), checkGroupIds);
            //将图片名称保存到Redis
//            savePic2Redis(setmeal.getImg());
            int i = setmealDao.updateSetmeal(setmeal);
            return i > 0 ? Result.success(MessageConstant.EDIT_SETMEAL_SUCCESS) : Result.error(MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            return Result.error();
        }
    }

    @Override
    public Result queryCheckGroupIdsBySetmealId(Integer setmealId) {
        List<Integer> checkGroupIds = setmealDao.selectCheckGroupIdsBySetmealId(setmealId);
        log.info("checkGroupIds:{}", checkGroupIds);
        if (checkGroupIds != null && checkGroupIds.size() > 0) {
            return Result.success(checkGroupIds, MessageConstant.QUERY_SETMEALLIST_SUCCESS);
        }
        return Result.error(MessageConstant.QUERY_SETMEALLIST_FAIL);
    }

    @Override
    public Result clearSetmealById(Integer id) {
        try {
            //根据检查组id删除中间表数据（清理原有关联关系）
            setmealDao.deleteAssociation(id);
            //不删除会员，只删预约该套餐的预约

            //根据套餐Id查询预约对象
            List<Order> orders = orderService.findOrderBySetMealId(id);
            log.info("根据套餐Id查询的预约对象列表:{}", orders);
            //预约其会员对象
            for (Order order : orders) {
                //根据预约对象删除
                OrderList orderList = new OrderList();
                //根据查询出来的会员ID查询会员对象，
                Member member = memberDao.findById(order.getMemberId());
                //封装其手机号码
                order.setPhoneNumber(member.getPhoneNumber());
                BeanUtils.copyProperties(order, orderList);
                System.out.println(orderList);
                orderService.deleteOrderSetting(orderList);
            }
            //删除预约对象
//            return Result.success();
            //删除检查组
            int i = setmealDao.deleteSetmeal(id);
            return i > 0 ? Result.success(MessageConstant.DELETE_SETMEAL_SUCCESS) : Result.error(MessageConstant.DELETE_SETMEAL_FAIL);
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error();
        }
    }

    private void setSetmealAndCheckGroup(Integer setmealId, Integer[] checkGroupIds) {
        //判断检查项目Id数组是否为空
        if (checkGroupIds != null && checkGroupIds.length > 0) {
            //添加检查组和检查项
            for (Integer checkGroupId : checkGroupIds) {
                setmealDao.insertSetmealAndCheckGroup(checkGroupId, setmealId);
            }
        }
    }

    /*//将图片名称保存到Redis
    private void savePic2Redis(String pic) {
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, pic);
    }*/

}
